package com.lwg.datastructure.dynamicPrograming;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @FileName: targetSum.java
 * @Description: 最大目标和
 * Copyright: Copyright(C) 2020-2022
 * Company  : Sunyard.hangzhou
 * @author: 李炜光
 * @slogan:
 * @Date: 2022/1/24 0024
 */
public class targetSum {


    public int findTargetSumWays(int[] nums, int target) {
        int length = nums.length;
        int sum = 0;
        //该数组所有数的和  capacity=(sum-target)/2
        for (int num : nums) {
            sum += num;
        }
        //capacity不能小于0  不能为奇数
        if (sum < target || ((sum - target) & 1) != 0) {
            return 0;
        }
        int capacity = (sum - target) >> 1;
        //初始化数组 int[3] 下标只有0,1,2
        //int dp[][] = new int[length + 1][capacity + 1];
        int dp[] = new int[capacity + 1];
        //当不选择数时 和为0的只有一种
        //dp[0][0] = 1;
        dp[0]=1;
        //从选一个数开始
        for (int i = 1; i <= length; i++) {
            for (int j = 0; j <= capacity; j++) {
                //如何当前数字小于或等于j时 可选可不选
                /*if (j >= nums[i - 1]) {
                    dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i - 1]];
                    System.out.println("dp["+(i-1)+"]["+j+"]--1--"+dp[i-1][j]);
                    System.out.println("dp["+(i-1)+"]["+(j - nums[i - 1])+"]--2--"+dp[i - 1][j - nums[i - 1]]);
                    System.out.println("dp["+i+"]["+j+"]--3--"+dp[i][j]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                    System.out.println("dp["+i+"]["+j+"]--4--"+dp[i][j]);
                }*/
            }
            for (int j = capacity; j >= 0; j--) {
                if (j >= nums[i - 1]) {
                    dp[j] += dp[j - nums[i - 1]];
                    System.out.println("dp["+(j - nums[i - 1])+"]--1--"+dp[j - nums[i - 1]]);
                    System.out.println("dp["+j+"]--2--"+dp[j]);
                }
            }

        }
        //return dp[length][capacity];
        return dp[capacity];
    }
}

class MainClass5 {
    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return new int[0];
        }

        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for (int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            int[] nums = stringToIntegerArray(line);
            line = in.readLine();
            int target = Integer.parseInt(line);

            int ret = new targetSum().findTargetSumWays(nums, target);

            String out = String.valueOf(ret);

            System.out.print(out);
        }
    }
}